﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;

public partial class Admin_UserControl_ucListLoanPhone : System.Web.UI.UserControl
{
    TblLoan2PhoneDAO loan2PhoneDAO = new TblLoan2PhoneDAO(Global.CONNECTION_STRING);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadLoanPhone();
        }
    }

    private void loadLoanPhone()
    {
        try
        {
            grdLoanPhone.DataSource = loan2PhoneDAO.GetAll();
            grdLoanPhone.DataBind();
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
    }
   
    protected void btnModelDelete_Click(object sender, ImageClickEventArgs e)
    {
        // string id = Request.QueryString["id"];
        bool flag = false;
        try
        {

            //TblModel model = new TblModel();
            TblLoan2Phone Lphone = new TblLoan2Phone();
            Lphone.LPhoneID = ((ImageButton)sender).CommandArgument;
            string IDDelete = ((ImageButton)sender).CommandArgument;
            loan2PhoneDAO.DeleteLPhone(IDDelete);
            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flag)
        {
            Response.Redirect("~/Admin/ListLoanPhone.aspx");
        }
    }

    protected void drStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {

            foreach (GridViewRow grd in grdLoanPhone.Rows)
            {
                DropDownList drStatus = (DropDownList)grd.FindControl("drStatus");
                HiddenField hidden = (HiddenField)grd.FindControl("hid");
                string t = hidden.Value;               
                string value = drStatus.SelectedValue;

                loan2PhoneDAO.Update(Convert.ToInt32(value), t);
                loadLoanPhone();
            }

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }

    }
    protected void grdLoanPhone_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            HiddenField hidItemID = (HiddenField)e.Row.Cells[0].FindControl("hid");
            hidItemID.Value = DataBinder.Eval(e.Row.DataItem, "LPhoneID").ToString();
           string id = hidItemID.Value;

            DropDownList drstatus1 = (DropDownList)e.Row.Cells[7].FindControl("drStatus");

            DataTable dt = loan2PhoneDAO.GetLPhoneByID(id);
           
            foreach (DataRow dr in dt.Rows)
            {
                int status = Convert.ToInt32(dr["LoanStatus"].ToString());
                if (status == 1)
                {
                    drstatus1.SelectedValue = "1";

                }
                else
                {
                    drstatus1.SelectedValue = "0";
                }
            }
           
        }
    }
}
